package com.wxdc.nmgyt.nmgytwydc.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.model.JcdInfoBean;

import java.util.List;

/**
 * Created by Administrator on 2018/4/26.
 *
 *  RecyclerView为每个item添加点击事件
 *  最终目的
 *    模拟ListView的setOnItemClickListener()方法，调用者只须调用类似于setOnItemClickListener的东西就能获得被点击item的相关数据。
 *  原理
 *     为RecyclerView的每个子item设置setOnClickListener，然后在onClick中再调用一次对外封装的接口，将这个事件传递给外面的调用者。
 *     而“为RecyclerView的每个子item设置setOnClickListener”在Adapter中设置。其实直接在onClick中也能完全处理item的点击事件，但是这样会破坏代码的逻辑。
 */

public class NavigationData_JzdInfo_RecyclerViewAdapter extends RecyclerView.Adapter<NavigationData_JzdInfo_RecyclerViewAdapter.MyViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<JcdInfoBean> mJcdInfoBeans;
    private RecycleViewOnItemClickListener mItemClickListener;

    public interface  RecycleViewOnItemClickListener{
        void onItemClick(int position);
    }

    public NavigationData_JzdInfo_RecyclerViewAdapter(Context context, List<JcdInfoBean> jcdInfoBeans) {
        mContext = context;
        mJcdInfoBeans = jcdInfoBeans;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.list_item_jcdinfo, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        JcdInfoBean jcdInfoBean = mJcdInfoBeans.get(position);
        holder.itemView.setTag(position);
        holder.bind(jcdInfoBean);
    }

    @Override
    public int getItemCount() {
        return mJcdInfoBeans == null ? 0 : mJcdInfoBeans.size();
    }

    @Override
    public void onClick(View view) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }
    //------------------------------------------------------------------------------------
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView mJcdTextView,mJcdFlagTextView;
        private JcdInfoBean obj;
        private View m_itemView;
        public MyViewHolder(View itemView) {
            super(itemView);
            this.m_itemView = itemView;
            mJcdTextView = itemView.findViewById(R.id.jcdname_txtview);
            mJcdFlagTextView = itemView.findViewById(R.id.jcdname_flag_txtview);
        }
        public void bind(JcdInfoBean jcdInfoBean){
            obj = jcdInfoBean;
            if (jcdInfoBean.getAuditFlag() == 1) {//已提交
                this.m_itemView.setEnabled(false);
                //Log.i("TAG", "bind:-------------- " + jcdInfoBean.getGEMNAME());
                mJcdFlagTextView.setTextColor(mContext.getResources().getColor(R.color.already_commit_color));
                mJcdFlagTextView.setText("(已提交)");
            } else if (jcdInfoBean.getAuditFlag() == 2){//待提交
                this.m_itemView.setEnabled(true);
                mJcdFlagTextView.setTextColor(mContext.getResources().getColor(R.color.wait_commit_color));
                mJcdFlagTextView.setText("(待提交)");
            }else{//未录入
                this.m_itemView.setEnabled(true);
                mJcdFlagTextView.setTextColor(mContext.getResources().getColor(R.color.wait_enter_color));
                mJcdFlagTextView.setText("(待录入)");
            }
            mJcdTextView.setText(jcdInfoBean.getGEMNAME());
        }
    }
    //------------------------------------------------------------------------------------------
    public void setItemClickListener(RecycleViewOnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

}
